package com.bwie.student.service.impl;

import com.bwie.common.domain.Friend;
import com.bwie.common.domain.request.FriendRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.student.config.FastUtil;
import com.bwie.student.mapper.FriendMapper;
import com.bwie.student.service.FriendService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {

    @Autowired
    private FriendMapper friendMapper;

    @Autowired
    private FastUtil fastUtil;

    @Override
    public Result<PageResult<Friend>> showFriend(FriendRequest friendRequest) {
        PageHelper.startPage(friendRequest.getPageNum(),friendRequest.getPageSize());

        List<Friend> friendList = friendMapper.showFriend(friendRequest);

        PageInfo<Friend> info = new PageInfo<>(friendList);

        return PageResult.toResult(info.getTotal(),friendList);
    }

    @Override
    public Result fastDfsUpload(MultipartFile multipartFile) {
        String url="";

        try {
            url=fastUtil.upload(multipartFile);
            if(!url.equals("")){
                return Result.success("http://106.14.33.194:8888/"+url,"图片上传成功");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return Result.error("图片上传失败");
    }

    @Override
    public Result insertFriend(FriendRequest friendRequest) {
        Integer integer = friendMapper.insertFriend(friendRequest);
        return Result.success(integer);
    }

    @Override
    public Result findById(String friendId) {
        Friend byId = friendMapper.findById(friendId);
        return Result.success(byId);
    }

    @Override
    public Result updateFriend(Friend friend) {
        Integer integer = friendMapper.updateFriend(friend);
        return Result.success(integer);
    }
}
